// pages/OrderPage/OrderPage.js
import {
  queryAllOrderList,
  queryPendingPaymentOrderList,
  queryPendingReceiptOrderList,
  queryFinishedOrderList,
  queryAfterSalesOrderList,
  queryPendingDeliveryOrderList
} from "../../service/order"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 搜索框文本
    searchBoxText: "",
    // Tab区域激活的索引
    activeIndex: 0,
    // 订单的当前页
    page: 1,
    // 订单页面大小
    pageSize: 10,
    // 订单列表
    orderList: [],
    // 是否处于加载中的状态
    isLoading: false,
    // 控制下拉刷新状态
    isRefreshing:false
  },
  /**
   * 加载订单数据
   * @param {Number} index Tab区域的索引
   */
  async loadOrderList(index) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let res = []
    const page = this.data.page
    const pageSize = this.data.pageSize
    switch (index) {
      case 0:
        // 请求全部数据
        res = await queryAllOrderList(page, pageSize)
        this.setData({
          orderList: [...(this.data.orderList || []), ...(res.data || [])]
        })
        break;
      case 1:
        // 请求未付款数据
        res = await queryPendingPaymentOrderList(page, pageSize)
        this.setData({
          orderList: [...(this.data.orderList || []), ...(res.data || [])]
        })
        break;
      case 2:
        // 请求待发货数据
        res = await queryPendingDeliveryOrderList(page, pageSize)
        this.setData({
          orderList: [...(this.data.orderList || []), ...(res.data || [])]
        })
        break;
      case 3:
        // 请求待收货数据
        res = await queryPendingReceiptOrderList(page, pageSize)
        this.setData({
          orderList: [...(this.data.orderList || []), ...(res.data || [])]
        })
        break;
      case 4:
        // 请求已完成数据
        res = await queryFinishedOrderList(page, pageSize)
        this.setData({
          orderList: [...(this.data.orderList || []), ...(res.data || [])]
        })
        break;
      case 5:
        // 请求售后数据
        res = await queryAfterSalesOrderList(page, pageSize)
        this.setData({
          orderList: [...(this.data.orderList || []), ...(res.data || [])]
        })
        break;
      default:
        // 未知情况
        break;
    }
    wx.hideLoading()
  },
  /**
   * 下拉刷新操作
   */
  async onRefresh(){
    this.data.page = 1
    this.setData({
      orderList:[]
    })
    await this.loadOrderList(this.data.activeIndex)
    this.setData({
      isRefreshing:false
    })
  },
  /**
   * 点击Tab区域
   */
  async onTapTabRegion(event) {
    const index = event.target.dataset.index
    if (index === undefined || index === this.data.activeIndex) return
    this.setData({
      activeIndex: index,
      orderList: []
    })
    this.data.page = 1
    await this.loadOrderList(index)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const index = Number(options.index)
    this.setData({
      activeIndex: index
    })
    await this.loadOrderList(index)
  },
  /**
   * 搜索框输入
   */
  onDetectSearchBoxInput(event) {
    this.data.searchBoxText = event.detail.value
  },
  /**
   * 上拉输入
   */
  async onReachBottom() {
    this.setData({
      isLoading: true
    })
    const index = this.data.activeIndex
    this.data.page = this.data.page + 1
    await this.loadOrderList(index)
    this.setData({
      isLoading: false
    })
  },
  /**
   *  检测到订单组件发出删除信号
   */
  onDetectOrderDeleted(event){
    const orderId = event.detail.orderId
    const newOrderList = this.data.orderList.filter(order=>order.orderId !== orderId)
    this.setData({
      orderList:newOrderList
    })
  }


})